<?php

namespace app\controller\admin\ec;


use think\Request;
use \think\facade\Validate;
use app\eslib\EsUtils;
use app\model\ec\BrandsModel;
use app\model\ec\CategoriesModel;

class Brands
{

    /**
     * 构造方法
     */
    public function __construct()
    {
        // $this->middleware('jwtauth.admin');
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        // 查询 KV 数据
        $mode = $request->param('_mode', '');
        $categoryId = intval($request->param('category_id', 0));
        if ($mode == 'dict') {
            return json(EsUtils::stdMessage(true, '', BrandsModel::getDictData($categoryId)));
        }

        // 查询列表数据
        $searchText = $request->param('search_text', '');
        $pageSize = intval($request->param('page_size', 10));
        $data = BrandsModel::dbView()->where(function ($query) use ($categoryId, $searchText) {
            if ($categoryId > 0) {
                $query->where('category_id', $categoryId);
            }
            if ($searchText) {
                $query->where(function ($query) use ($searchText) {
                    $query->where('id', intval($searchText))->whereOr('brand_name', 'like', "%{$searchText}%");
                });
            }
        })->paginate($pageSize);
        return json(EsUtils::stdMessage(true, '', $data));
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $formData = $request->all();
        $validator = Validate::rule([
            'brand_name' => 'required|max:150',
            'icon' => 'required|max:150',
        ], [
            'required' => ':attribute 必须填写',
            'numeric' => ':attribute 必须是数值',
            'max' => ':attribute 不能超过 :max 个字符',
        ], [
            'brand_name' => '品牌名称',
            'icon' => '品牌图片'
        ]);
        if (!$validator->check($formData)) {
            return json(EsUtils::stdMessage(false, $validator->getError()));
        }
        // 表单数据处理
        // ...
        try {
            $model = BrandsModel::create($formData);
            return json(EsUtils::stdMessage(true, '', $model->id));
        } catch (\Exception $ex) {
            return json(EsUtils::stdMessage(false, $ex->getMessage()));
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        if (!is_numeric($id)) {
            return json(EsUtils::stdMessage(false, 'id值无效', $id));
        }
        if ($id > 0) {
            $model = BrandsModel::find($id);
            if (!$model) {
                return json(EsUtils::stdMessage(false, '记录不存在'));
            }
        } else {
            $model = new BrandsModel();
            $model->status = 1;
            $model->sort = 999;
        }
        $data = [
            'data' => $model,
            'status_data' => $model->getStatusDataAttr(),
            'category_id_data' => CategoriesModel::getDictData(),
        ];
        return json(EsUtils::stdMessage(true, $id, $data));
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $formData = $request->all();
        $validator = Validate::rule([
            'brand_name' => 'required|max:150',
            'icon' => 'required|max:150',
        ], [
            'required' => ':attribute 必须填写',
            'numeric' => ':attribute 必须是数值',
            'max' => ':attribute 不能超过 :max 个字符',
        ], [
            'brand_name' => '品牌名称',
            'icon' => '品牌图片'
        ]);
        if (!$validator->check($formData)) {
            return json(EsUtils::stdMessage(false, $validator->getError()));
        }
        try {
            $affected = BrandsModel::where('id', $id)->update($formData);
            return json(EsUtils::stdMessage(true, '', $affected));
        } catch (\Exception $ex) {
            return json(EsUtils::stdMessage(false, $ex->getMessage()));
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        try {
            $affected = BrandsModel::destroy($id);
            return json(EsUtils::stdMessage(true, '', $affected));
        } catch (\Exception $ex) {
            return json(EsUtils::stdMessage(false, $ex->getMessage()));
        }
    }
}
